"""
产品模型
"""
from datetime import datetime
from app.extensions import db


class Product(db.Model):
    """产品模型"""
    __tablename__ = 'product'
    
    id = db.Column(db.Integer, primary_key=True)
    workshop = db.Column(db.String(100), nullable=False)  # 车间
    product_name = db.Column(db.String(200), nullable=False)  # 产品名称
    nominal_grade = db.Column(db.String(50), nullable=False)  # 标称档位
    merged_grade = db.Column(db.String(50), nullable=False)  # 合并档位
    merge_file = db.Column(db.String(100), nullable=False)  # 合并档
    is_active = db.Column(db.Boolean, default=True)  # 是否启用
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'workshop': self.workshop,
            'product_name': self.product_name,
            'nominal_grade': self.nominal_grade,
            'merged_grade': self.merged_grade,
            'merge_file': self.merge_file,
            'is_active': self.is_active,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S'),
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S')
        }